package com.yonyou.cyxdms.sysAuth.service.controller.manage.user;

import java.util.List;
import java.util.Map;
import java.util.Set;

import com.yonyou.cyxdms.sysAuth.service.dto.user.UserAndRoleClaimDTO;
import com.yonyou.cyxdms.sysAuth.service.vo.user.UserEmpVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.util.UriComponentsBuilder;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yonyou.cyx.framework.compent.redis.RedisClient;
import com.yonyou.cyx.framework.util.bean.ApplicationContextHelper;
import com.yonyou.cyxdms.framework.bean.dto.user_info.LoginInfoDto;
import com.yonyou.cyxdms.sysAuth.service.dto.role.RolePDto;
import com.yonyou.cyxdms.sysAuth.service.dto.user.UserAndRoleDTO;
import com.yonyou.cyxdms.sysAuth.service.dto.user.UserParamDTO;
import com.yonyou.cyxdms.sysAuth.service.dto.user.UserPermissionActionListDTO;
import com.yonyou.cyxdms.sysAuth.service.service.UserService;
import com.yonyou.cyxdms.sysAuth.service.service.Login.UserLoginService;
import com.yonyou.cyxdms.sysAuth.service.vo.employee.EmployeeAddVO;
import com.yonyou.cyxdms.sysAuth.service.vo.user.CompanyUserVO;
import com.yonyou.cyxdms.sysAuth.service.vo.user.DealerUserVO;
import com.yonyou.cyxdms.sysAuth.service.vo.user.UserVO;
import com.yonyou.cyxdms.sysmanage.client.domains.dto.UserDictDto;
import com.yonyou.cyxdms.sysmanage.client.vo.employee.UserInfoVO;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@Api(value = "用户管理控制类", tags = {"用户管理控制类"})
@RestController
@RequestMapping("/basedata/users")
public class UserManageController {

    @Autowired
    private UserService userService;

    @Autowired
    private RedisClient redisClient;
    @Autowired
    private UserLoginService userLoginService;

    /**
     * 测试
     * TODO description
     * @param employeeId
     * @return
     * @author adu
     * @date 2018年10月18日
     */
    @RequestMapping(value = "/userDataVue/{employeeId}/{dataSource}", method = RequestMethod.GET)
    public Map<String, Object> userDataVue(@PathVariable(value = "employeeId") String employeeId, @PathVariable(value = "dataSource") String dataSource) {
        return userService.userDataVue(employeeId, dataSource);
    }


    /**
     * 根据用户的userOrgId 查询用户权限
     *
     * @author 李贺
     * @date 2019年9月23日
     */
    @RequestMapping(value = "/userPermission/{userOrgId}/{dataSource}", method = RequestMethod.GET)
    @ApiImplicitParams({@ApiImplicitParam(value = "用户id", name = "userId", required = true, dataType = "string", paramType = "header")})
    public Map<String, Object> userPermission(@PathVariable(value = "userOrgId") Long userOrgId, @PathVariable(value = "dataSource") String dataSource) {
        return userService.userPermission(userOrgId, dataSource);
    }


    /**
     * 查询登录用户信息
     *
     * @return
     * @author 李贺
     * @date 2019年9月18日
     */
    @RequestMapping(value = "/loginuser", method = RequestMethod.GET)
    @ApiOperation(value = "查询登录用户信息 pc迁移")
    @ApiImplicitParams({@ApiImplicitParam(value = "用户id", name = "userId", required = true, dataType = "string", paramType = "header")})
    public EmployeeAddVO findByEmployeeId() {
        return userService.findByEmployeeId();
    }

    /**
     * 快速修改密码
     *
     * @param uricb
     * @return
     * @author 李贺
     * @date 2019年9月19日
     */
    @RequestMapping(value = "/editpassword", method = RequestMethod.PUT)
    @ApiOperation(value = "快速修改密码")
    @ApiImplicitParams({@ApiImplicitParam(value = "用户id", name = "userId", required = true, dataType = "string", paramType = "header")})
    public ResponseEntity<UserVO> editpassword(@RequestBody UserVO userDto, UriComponentsBuilder uricb,@RequestHeader(value = "userLoginId",required = false)Long userLoginId) {
        Long id = userService.editpassword(userDto);
        MultiValueMap<String, String> headers = new HttpHeaders();
        headers.set("Location", uricb.path("/basedata/users/editpassword").buildAndExpand(id).toUriString());
        LoginInfoDto loginInfoDto = ApplicationContextHelper.getBeanByType(LoginInfoDto.class);
        Object o = redisClient.get(loginInfoDto.getUserId().toString());
        if (o != null) {
            String jwt = (String) o;
            userLoginService.logout(jwt,userLoginId);
        }
        return new ResponseEntity<>(headers, HttpStatus.CREATED);
    }

    /**
     * @author ：adu
     * @date ：2019/8/30 15:03
     * @description：用户权限管理：修改密码
     */
    @RequestMapping(value = "/supereditpassword", method = RequestMethod.PUT)
    @ApiOperation(value = "用户权限管理：修改密码")
    @ApiImplicitParams({@ApiImplicitParam(value = "用户id", name = "userId", required = true, dataType = "string", paramType = "header")})
    public ResponseEntity<UserVO> supereditpassword(@RequestBody UserVO userDto, UriComponentsBuilder uricb) {
        Long id = userService.supereditpassword(userDto);
        MultiValueMap<String, String> headers = new HttpHeaders();
        headers.set("Location", uricb.path("/basedata/users/supereditpassword").buildAndExpand(id).toUriString());
        return new ResponseEntity<>(headers, HttpStatus.CREATED);
    }

    /**
     * 用户对应的权限的新增及修改
     *
     * @param RolePDto
     * @return
     * @throws Exception
     * @author maxingjian
     * @date 2019年7月12日
     */
    @RequestMapping(value = "/permission", method = RequestMethod.PUT)
    @ApiImplicitParams({@ApiImplicitParam(value = "用户id", name = "userId", required = true, dataType = "string", paramType = "header")})
    public RolePDto modifyOrg(@RequestBody RolePDto RolePDto)
            throws Exception {
        return userService.modifyOrg(RolePDto);
    }

    /**
     * TODO description
     *
     * @param userId
     * @return
     * @author maxingajin
     * @date 2019年08月13日
     */
    @RequestMapping(value = "/getUserById", method = RequestMethod.GET)
    public UserDictDto getUserById(@ApiParam(name = "userId", value = "用户id", required = true) @RequestParam("userId") Long userId) {
        return userService.getUserById(userId);
    }

    @RequestMapping(value = "/getUserByIdCMS", method = RequestMethod.GET)
    public UserDictDto getuserbyidcms(@ApiParam(name = "userId", value = "用户id", required = true) @RequestParam("userId") Long userId) {
        return userService.getuserbyidcms(userId);
    }

    @RequestMapping(value = "/getUserByIdCMSNew", method = RequestMethod.GET)
    public UserDictDto getuserbyidcmsnew(@ApiParam(name = "userId", value = "用户id", required = true) @RequestParam("userId") Long userId
    , @ApiParam(name = "userOrgId", value = "职位ID", required = true) @RequestParam("userOrgId") String userOrgId) {
        return userService.getuserbyidcmsnew(userId,userOrgId);
    }


    /**
     * 根据角色类别获取用户
     *
     * @param role 角色类别
     * @return 用户
     * @author 李贺
     * @date 2019年9月4日
     */
    @RequestMapping(value = "/getUserListByRole", method = RequestMethod.GET)
    @ApiOperation(value = "根据角色类别获取用户")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "userId", name = "userId", required = true, dataType = "string", paramType = "header")
    })
    public List<UserAndRoleDTO> getUserListByRole(@ApiParam(name = "role", value = "角色类别", required = false) @RequestParam(value = "role") Integer role
    ) {
        return userService.getUserListByRole(role);
    }

    /**
     * 提供索赔接口 技术支持
     *
     * @param role 角色类别
     * @return
     * @author 谢志恺
     * @since 2021/3/5
     */
    @RequestMapping(value = "/getUserListByRoleClaim", method = RequestMethod.GET)
    @ApiOperation(value = "提供索赔接口 技术支持")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "userId", name = "userId", required = true, dataType = "string", paramType = "header")
    })
    public List<UserAndRoleClaimDTO> getUserListByRoleClaim(@ApiParam(name = "role", value = "角色类别", required = false) @RequestParam(value = "role") String role
    ) {
        return userService.getUserListByRoleClaim(role);
    }


    /**
     * 根据用户id获取用户全部信息
     *
     * @param userId 角色类别
     * @return 用户
     * @author maxingjain
     * @date 2019年9月20日
     */
    @RequestMapping(value = "/getUserInfoByUserId", method = RequestMethod.GET)
    @ApiOperation(value = "根据用户id获取用户全部信息")
    public UserInfoVO getUserInfoByUserId(@ApiParam(name = "userId", value = "用户id", required = true) @RequestParam("userId") Long userId) {
        return userService.getUserInfoByUserId(userId);
    }

    @RequestMapping(value = "/getUserInfoList",method = RequestMethod.POST)
    @ApiOperation(value = "获取用户全部信息")
    @ApiImplicitParams({
        @ApiImplicitParam(value = "userId", name = "userId", required = true, dataType = "string", paramType = "header")
})
    public List<UserInfoVO> getUserInfoList(@RequestBody Set<String> userIdSet) {
        return userService.getUserInfoList(userIdSet);
    }


    /**
     * 根据条件用户全部信息
     *
     * @param
     * @return 用户
     * @author maxingjain
     * @date 2019年9月20日
     */
    @RequestMapping(value = "/getUserInfoByParams", method = RequestMethod.GET)
    @ApiOperation(value = "根据条件用户全部信息")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "userId", name = "userId", required = true, dataType = "string", paramType = "header")
    })
    public IPage<UserInfoVO> getUserInfoByParams(@ApiParam(name = "pageNum", value = "当前页") @RequestParam("pageNum") Long current,
                                                 @ApiParam(name = "limit", value = "分页大小") @RequestParam("limit") Long size,
                                                 UserParamDTO param) {
        Page<UserInfoVO> page = new Page(current, size);
        return userService.getUserInfoByParams2(page, param);
    }

    /**
     * 获取用户的按钮菜单权限
     *
     * @param
     * @return 按钮菜单
     * @author maxingjain
     * @date 2019年9月26日
     */
    @RequestMapping(value = "/getUserPermissionAction", method = RequestMethod.GET)
    @ApiOperation(value = "获取用户的按钮菜单权限")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "userId", name = "userId", required = true, dataType = "string", paramType = "header")
    })
    public Map<String, List<UserPermissionActionListDTO>> getUserPermissionAction() {
        return userService.getUserPermissionAction();
    }

    /**
     * 获得当前经销商下所有人员信息
     */
    @RequestMapping(value = "/getDealerUser", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(value = "userId", name = "userId", required = true, dataType = "string", paramType = "header")
    })
    @ApiOperation(value = "获得当前经销商下所有人员信息")
    public List<DealerUserVO> getDealerUser() {
        return userService.getDealerUser();
    }

    /**
     * 获得当前公司所有人员
     * @author 马邢健
     * @since 2019/12/31
     */
    @ApiOperation(value = "获得当前公司所有人员")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "userId", name = "userId", required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(value = "appId", name = "appId", required = true, dataType = "string", paramType = "header")
    })
    @GetMapping("/getAllUser")
    public List<CompanyUserVO> getAllUser(){
        return userService.getAllUser();
    }


    /**
     * 获得当前用户可操作的人员
     *
     * @author 李贺
     * @since 2019/10/18
     */
    @ApiOperation(value = "获得当前用户可操作的人员")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "userId", name = "userId", required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(value = "appId", name = "appId", required = true, dataType = "string", paramType = "header")
    })
    @GetMapping("/getUsersByPosition")
    public List<com.yonyou.cyxdms.sysmanage.client.vo.user.UserInfoVO> getUsersByPosition(@ApiParam(name = "menuId", value = "菜单Id") @RequestParam(value = "menuId") Long menuId,
                                                                                          @RequestParam(value = "positionCodes") List<Integer> positionCodes,@RequestParam(value = "userPhone",required = false) String userPhone) {
        return userService.getUsersByPosition(menuId, positionCodes, userPhone);
    }

    /**
     * 网销经理或网销顾问查询所属顾问
     *
     * @author wy
     * @since 2020/04/06
     */
    @ApiOperation(value = "获网销经理或网销顾问查询所属顾问")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "userId", name = "userId", required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(value = "appId", name = "appId", required = true, dataType = "string", paramType = "header")
    })
    @GetMapping("/getUsersByManager")
    public List<com.yonyou.cyxdms.sysmanage.client.vo.user.UserInfoVO> getUsersByManager(@ApiParam(name = "menuId", value = "菜单Id") @RequestParam(value = "menuId") Long menuId,
                                                                                          @RequestParam(value = "positionCodes") List<Integer> positionCodes,@RequestParam(value = "userPhone",required = false) String userPhone) {
        return userService.getUsersByManager(menuId, positionCodes, userPhone);
    }

    /**
     * 根据userId获得该用户当前登录的职位信息
     */
    @ApiOperation(value = "根据userId获得该用户当前登录的职位信息")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "userId", name = "userId", required = true, dataType = "string", paramType = "header")
    })
    @GetMapping("/getAppPosition")
    public Integer getAppPosition() {
        return userService.getAppPosition();
    }


    /**
     * 根据userId获得该用户的个人参数配置
     */
    @ApiOperation(value = "根据登录用户获得该用户的个人参数配置")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "userId", name = "userId", required = true, dataType = "string", paramType = "header")
    })
    @GetMapping("/getUserPermissionCtrl")
    public Map<String,List<String>> getUserPermissionCtrl() {
        return userService.getUserPermissionCtrl();
    }




    /**
     * 通过userid获取经理的userid
     */
    @ApiOperation(value = "通过userid等获取经理的userid")
    @GetMapping("/getManagerIdByUserId")
        public List<String> getManagerIdByUserId(@ApiParam(name = "userId", value = "用户id", required = true) @RequestParam(value = "userId") String userId,
                                          @ApiParam(name = "positionCode", value = "角色类别", required = true) @RequestParam(value = "positionCode") String positionCode,
                                          @ApiParam(name = "orgId", value = "组织id", required = true) @RequestParam(value = "orgId") String orgId)
    {
        return userService.getManagerIdByUserId(userId,positionCode,orgId);
    }

    /**
     * 根据userName获得userId
     */
    @ApiOperation(value = "根据userName获得userId")
    @GetMapping("/getUserIdByName")
    public List<Long> getUserIdByName(@ApiParam(name = "appId", value = "appId", required = true) @RequestParam("appId")String appId,
                                      @ApiParam(name = "ownerCode", value = "ownerCode", required = true) @RequestParam("ownerCode")String ownerCode,
                                      @ApiParam(name = "name", value = "name", required = true) @RequestParam("name")String name) {
        return userService.getUserIdByName(appId, ownerCode, name);
    }


    /**
     * 根据userName获得userId
     */
    @ApiOperation(value = "根据userName获得userId")
    @GetMapping("/getUserIdByNameAndPositionCode")
    public List<Long> getUserIdByNameAndPositionCode(@ApiParam(name = "appId", value = "appId", required = true) @RequestParam("appId")String appId,
                                      @ApiParam(name = "ownerCode", value = "ownerCode", required = true) @RequestParam("ownerCode")String ownerCode,
                                      @ApiParam(name = "name", value = "name", required = true) @RequestParam("name")String name) {
        return userService.getUserIdByNameAndPositionCode(appId, ownerCode, name);
    }

    /**
     * 根据当前人的职位id获取角色类别
     * @author 马邢健
     * @since 2019/12/31
     */
    @ApiOperation(value = "根据当前人的职位id获取角色类别")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "userId", name = "userId", required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(value = "appId", name = "appId", required = true, dataType = "string", paramType = "header")
    })
    @GetMapping(value="/getPositionByUserOrgId",produces = "application/json;charset=UTF-8")
    public String getPositionByUserOrgId(@ApiParam(name = "userOrgId", value = "userOrgId", required = true) @RequestParam(value = "userOrgId") Long userOrgId){
        return userService.getPositionCodeByUserOrgId(userOrgId);
    }
    /**
     * 根据userId获取用户名
     */
    @ApiOperation(value = "根据userId获取用户名")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "userIds", name = "userIds", required = true, dataType = "string", paramType = "header"),
    })
    @GetMapping(value="/getNameByIds",produces = "application/json;charset=UTF-8")
    public List<UserEmpVO> getNameByIds(@RequestParam(value = "userIds") String userIds){
        return userService.getNameByIds(userIds);
    }

    /**
     * 获取所有用户名
     */
    @ApiOperation(value = "获取所有用户名")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "userIds", name = "userIds", required = true, dataType = "string", paramType = "header"),
    })
    @GetMapping(value="/getNameByAppId",produces = "application/json;charset=UTF-8")
    public List<UserEmpVO> getNameByAppId(@RequestParam(value = "appId") String appId){
        return userService.getNameByAppId(appId);
    }
    /**
     * 按职位查人员
     */
    @GetMapping(value="/findUserByRole",produces = "application/json;charset=UTF-8")
    public List<UserEmpVO> findUserByRole(@RequestParam(value = "roleCode",required = false) String roleCode,
                                          @RequestParam(value = "roleId",required = false) String roleId,
                                          @RequestParam(value = "ownerCode",required = false) String ownerCode,
                                          @RequestParam(value = "companyId",required = false) String companyId,
                                          @RequestParam(value = "appId",required = false) String appId){
        return userService.findUserByRole(roleCode,roleId,ownerCode,companyId,appId);
    }
}
